package com.hellojava.util.web;

import com.hellojava.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonResponse {

    static Logger logger = LoggerFactory.getLogger(JsonResponse.class);
    private ResponseHeader header;

    private String msg;

    private Object data;

    private JsonResponse(ResponseHeader header, String msg, Object data) {
        this.header = header;
        this.msg = msg;
        this.data = data;
    }

    public static JsonResponse newInstance(ResponseHeader header, String msg, Object data) {
        logger.info(" \n返回客户端json :\n "+ JsonUtil.beanToJson(data));
        return new JsonResponse(header, msg, data);
    }

    public static JsonResponse newInstance(ResponseHeader header, Object data) {
        return new JsonResponse(header, null, data);
    }

    public Integer getCode() {
        return header.getCode();
    }

    public String getMsg() {
        return msg != null ? msg : header.getDescription();
    }

    public Object getData() {
        return data;
    }
}
